package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.TicketMapper;
import com.qf.dao.TicketRepository;
import com.qf.pojo.*;
import com.qf.service.ITicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Author
 * @Description
 * @Date 2022/3/28
 */
@Service
public class TicketServiceImpl implements ITicketService {

    @Autowired
    TicketMapper ticketMapper;
    @Autowired
    TicketRepository ticketRepository;





    @Override
    public BaseResp findById(Integer id) {

        Ticket byId = ticketMapper.findById(id);

        if (byId != null) {
            return new BaseResp(1, "余票查询成功！", byId, null);
        }
        return new BaseResp(0, "余票查询失败！", null, null);

    }


    @Override
    public BaseResp update(UpdateRes updateRes) {

        Integer ticketId = updateRes.getTicketId();
        Integer trainSeatType = updateRes.getTrainSeatType();
        Integer updateType = updateRes.getUpdateType();

        if (ticketId != null && trainSeatType != null && updateType != null) {



            Ticket byId = ticketMapper.findById(ticketId);

            Message message = byId.getMessage();
            Message messageNew = new Message();
            if (trainSeatType == 1) {
                Integer superSeatOld = message.getSuperSeat();
                Integer messageIdOld = message.getMessageId();
                Integer superSeatNew = superSeatOld + updateType;
                messageNew.setSuperSeat(superSeatNew);
                messageNew.setMessageId(messageIdOld);
                ticketRepository.saveAndFlush(messageNew);
            }
            if (trainSeatType == 2) {
                Integer firstSeatOld = message.getFirstSeat();
                Integer messageIdOld = message.getMessageId();
                Integer firstSeatNew = firstSeatOld + updateType;
                messageNew.setFirstSeat(firstSeatNew);
                messageNew.setMessageId(messageIdOld);
                ticketRepository.saveAndFlush(messageNew);
            }
            if (trainSeatType == 3) {
                Integer secondSeatOld = message.getSecondSeat();
                Integer messageIdOld = message.getMessageId();
                Integer secondSeatNew = secondSeatOld + updateType;
                messageNew.setSecondSeat(secondSeatNew);
                messageNew.setMessageId(messageIdOld);
                ticketRepository.saveAndFlush(messageNew);
            }
            return new BaseResp(1, "余票修改成功！", null, null);
        }
        return new BaseResp(0, "余票修改的数据参数错误", null, null);
    }

    @Override
    public BaseResp findAll(TicketRep tickerRep) {
        System.out.println(tickerRep.toString());
        String endStand = tickerRep.getEndStand();
        String endData = tickerRep.getEndTime();
        String startStand = tickerRep.getStartStand();
        String startData = tickerRep.getStartTime();

        List<Ticket> all = ticketMapper.findAll(endStand, startStand, endData, startData);


        PageInfo<Ticket> ticketPageInfo = new PageInfo<>(all);
        long total = ticketPageInfo.getTotal();
        if (all != null) {
            return new BaseResp(0, "余票查询成功！", all, null);
        }
        return new BaseResp(1, "余票查询失败！", null, null);

    }
}
